//
//  HMSinaWelComeController.swift
//  ZJCSina
//
//  Created by 朱建超 on 16/11/17.
//  Copyright © 2016年 itHeiMa. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class HMSinaWelComeController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()

    }
    
    override func viewDidAppear(_ animated: Bool) {
        
        super.viewDidAppear(animated)
        
        startAnimtion()
        
    }
    
    //实现动画
    private func startAnimtion() {
        
        UIView.animate(withDuration: 1.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 3, options: [], animations: {
            
            self.iconView.snp.updateConstraints({ (make) in
                
                make.bottom.equalTo(self.view.snp.bottom).offset(-400)
                
            })
            
            self.view.layoutIfNeeded()
            
        }) { (finished) in
            
            UIView.animate(withDuration: 0.5, animations: {
                
                self.welcomeLabel.alpha = 1
                
            }, completion: { (_) in
                print("动画完成")
                
                NotificationCenter.default.post(name: NSNotification.Name(chooseRootViewController), object: nil)
                
            })
            
        }
        
    }
    
    //搭建界面
    private func setupUI() {
        
        //设置背景颜色
        view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        
        self.view.addSubview(iconView)
        self.view.addSubview(welcomeLabel)
        
        iconView.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(self.view.snp.centerX)
            make.bottom.equalTo(self.view.snp.bottom).offset(-150)
            make.size.equalTo(CGSize(width: 80, height: 80))
            
        }
        
        welcomeLabel.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(iconView.snp.centerX)
            make.top.equalTo(iconView.snp.bottom).offset(20)
            
        }
        
        //影藏文字
        welcomeLabel.alpha = 0
        
        //设置iconImage为圆角
        iconView.layer.cornerRadius = 40
        iconView.layer.masksToBounds = true
        
        //设置网路图片
        iconView.sd_setImage(with: HMSinaUserAccountViewModel.shared.headURL)
    }
    
    //实例化控件
    private lazy var iconView : UIImageView = UIImageView(image: #imageLiteral(resourceName: "avatar_default_big"))
    
    private lazy var welcomeLabel : UILabel = UILabel(title: "欢迎归来", font: 16, textColor: UIColor.darkGray)

}
